import AudioManager from "./AudioManager";
import PeripheralSystemDataMgr from "./PeripheralSystemDataMgr";
import EventMgr from "./EventMgr";
import GameEventType from "./GameEventType";
import AdManager from "./AdManager";
import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import PageMgr from "./PageMgr";
import PeripheralSystemMgr, { TravelEvent } from "./PeripheralSystemMgr";
import UiManage from "./UiManage";
import TravelPropEventPage from "./TravelPropEventPage";
import NativeUtils from "./NativeUtils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TravelPropEventPageCtrl extends BasePageCtrl {
    ui = null;

    _animType: AnimType = AnimType.LEFT_TO_RIGHT;
    _touchControl: boolean = false;
    _hasPeneLock: boolean = true;
    _hasBlack: boolean = true;
    _hasTouchLock: boolean = false;
    _hasBlackTouch: boolean = false;
    _showWinSize: boolean = true;

    onLoad() {
        this.onUILoad();
        this.addButtonListen();
        super.onLoad();
    }

    start() {}

    _init() {
        AudioManager.getInstance().playMusic("eventOut");
    }

    onUILoad() {
        this.ui = this.node.addComponent(TravelPropEventPage);
    }

    addButtonListen() {
        UiManage.addButtonListen(this.ui.btn_noNeed, this.clickClose, this, 0, "cost_heart");
        UiManage.addButtonListen(this.ui.btn_video, this.clickVideo, this);
    }

    clickClose() {
        this.hide();
        PeripheralSystemDataMgr.checkNextOutSideGuide();
        PeripheralSystemDataMgr.checkShowGameStartPage();
    }

    clickVideo() {
        var self = this;
        PeripheralSystemDataMgr.reportEncourageVideo();
        NativeUtils.showVideoAd(7,function () {
            PeripheralSystemMgr.getVideoReward(TravelEvent.SKILL, 1, function () {
                self.hide();
                PageMgr.showPage("SkillRewardPage");
            });
        });
    }

    playAddSkillAction() {
        const skills = [5, 6, 7];
        const skillNodes = [this.ui.hammer, this.ui.bowAndArrow, this.ui.artillery];

        for (let i = 0; i < skills.length; i++) {
            const skillId = skills[i];
            const skillPos = skillNodes[i].getPosition();
            const parentNode = skillNodes[i].node.parent;
            const delay = 0.25 * i;

            cc.tween(parentNode)
                .delay(delay)
                .call(function () {
                    EventMgr.trigger(GameEventType.SHOWEFFECT_FLYINGRED, {
                        startPos: skillPos,
                        type: skillId,
                        callback: function () {
                            if (i === skills.length - 1) {
                                PeripheralSystemDataMgr.checkNextOutSideGuide();
                                PeripheralSystemDataMgr.checkShowGameStartPage();
                            }
                        }
                    });
                })
                .start();
        }
    }
}

// TravelPropEventPageCtrl.prefabUrl = "TravelPropEventPage";
// TravelPropEventPageCtrl.className = "TravelPropEventPageCtrl";
